package com.gushi.POICreateExcel;

import org.apache.poi.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import com.gushi.POICreateExcel.excelAnnotation.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import com.gushi.POICreateExcel.cellStyle.*;

public class gsExcel {

    /**
     * 根据类型获取Excel表的Sheet名
     * @param c 传入的类型
     * @return Sheet名
     */
    public static String getExcelSheetName (Class c) {

        anttnExcelSheet annotation = (anttnExcelSheet) c.getAnnotation(anttnExcelSheet.class);

        if (annotation != null) {
            return annotation.name();
        }
        else {
            return c.getName();
        }
    }

    /**
     * 根据类型获取Excel表的列名
     * @param c 传入的类型
     * @return 列名数组
     */
    public static String[] getExcelColNames (Class c) {

        Field[] fields = c.getDeclaredFields();
        String[] colNames = new String[fields.length];

        for (int i = 0; i < colNames.length; ++i) {

            Field curField = fields[i];
            anttnExcelColumn annotation = curField.getAnnotation(anttnExcelColumn.class);
            if (annotation != null) {
                colNames[i] = annotation.name();
            }
            else {
                colNames[i] = curField.getName();
            }
        }

        return colNames;
    }


    /**
     * 根据对象数组创建一个Excel文件
     * @param array 对象数组
     * @param path 需生成的Excel文件路径
     */
    public static void createExcelFromObjectArray (Object[] array, String path) {

        String sheetName = getExcelSheetName(array.getClass().getComponentType());
        String[] colNames = getExcelColNames(array.getClass().getComponentType());

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        Sheet sheet = hssfWorkbook.createSheet(sheetName);



        HSSFCellStyle nameCellStyle = new gsCellStyle(hssfWorkbook)
                .setBold()
                .setFontSize(20)
                .setCenter()
                .getCellStyle();

        HSSFCellStyle remarkCellStyle = new gsCellStyle(hssfWorkbook)
                .setBold()
                .setRight()
                .getCellStyle();

        HSSFCellStyle headCellStyle = new gsCellStyle(hssfWorkbook)
                .setFontSize(14)
                .setBold()
                .setBorder()
                .getCellStyle();

        HSSFCellStyle bodyCellStyle = new gsCellStyle(hssfWorkbook)
                .setBorder()
                .getCellStyle();


        int rowCursor = 0;


        Row nameRow = sheet.createRow(rowCursor++);
        Cell nameCell = nameRow.createCell(0);
        nameCell.setCellValue(sheetName);
        nameCell.setCellStyle(nameCellStyle);

        // 合并单元格
        CellRangeAddress cra =new CellRangeAddress(0, 0, 0, colNames.length - 1); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra);

        Row remarkRow = sheet.createRow(rowCursor++);
        Cell remarkCell = remarkRow.createCell(0);
        remarkCell.setCellValue("2017年10月16日15:23:02");
        remarkCell.setCellStyle(remarkCellStyle);

        // 合并单元格
        CellRangeAddress cra2 =new CellRangeAddress(1, 1, 0, colNames.length - 1); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra2);


        //创建首行
        Row headRow = sheet.createRow(rowCursor++);
        for (int x = 0; x < colNames.length; ++x) {

            sheet.setColumnWidth(x, 4000);

            Cell cell = headRow.createCell(x);
            cell.setCellStyle(headCellStyle);
            cell.setCellValue(colNames[x]);
        }

        //创建内容行
        int rowCursorOp = rowCursor;
        for (int y = 0; y < array.length; ++y) {

            Object curObj = array[y];
            Row bodyRow = sheet.createRow(rowCursorOp + y);
            bodyRow.setHeight((short)320);
            String[] values = gsReflectionTool.getPropValueStrings(curObj);

            for (int x = 0; x < values.length; ++x) {

                Cell cell = bodyRow.createCell(x);
                cell.setCellValue(values[x]);
                cell.setCellStyle(bodyCellStyle);
            }

            rowCursor++;
        }

        try {

            FileOutputStream fileOutputStream = new FileOutputStream(path);
            hssfWorkbook.write(fileOutputStream);
            hssfWorkbook.close();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
